package com.hdd.biz.painting.controller;


import com.hdd.biz.painting.dto.WorksListRequest;
import com.hdd.biz.painting.dto.WorksRequest;
import com.hdd.biz.painting.service.PaintingWorksService;
import com.hdd.dto.SuperResponse;
import com.hdd.utils.wx.WxMiniProgramUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.OutputStream;

/**
 * <p>
 * 作品表 前端控制器
 * </p>
 *
 * @author fang
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/painting")
@Api(value = "作品模块",tags = {"作品模块"})
@CrossOrigin(origins = "*",maxAge = 3600)
public class PaintingWorksController {

    @Autowired
    private PaintingWorksService  paintingWorksService;
    @Autowired
    private WxMiniProgramUtil wxMiniProgramUtil;


    @PostMapping("/list")
    @ApiOperation(value = "作品列表" ,notes = "")
    public SuperResponse list(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.worksList(request);
    }

    @PostMapping("/listForMobile")
    @ApiOperation(value = "作品列表(移动端)" ,notes = "")
    public SuperResponse listForMobile(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.listForMobile(request);
    }


    @PostMapping("/recommend")
    @ApiOperation(value = "推荐" ,notes = "")
    public SuperResponse recommend(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.recommend(request);
    }

    @PostMapping("/setTop")
    @ApiOperation(value = "是否置顶" ,notes = "")
    public SuperResponse setTop(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.setTop(request);
    }

    @PostMapping("/info")
    @ApiOperation(value = "作品详情" ,notes = "")
    public SuperResponse info(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.worksInfo(request);
    }

    @PostMapping("/adopt")
    @ApiOperation(value = "作品审核通过" ,notes = "")
    public SuperResponse adopt(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.adopt(request);
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除" ,notes = "")
    public SuperResponse delete(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.delete(request);
    }


    @PostMapping("/preview")
    @ApiOperation(value = "预览(查看小程序码)" ,notes = "")
    public void preview(@Valid @RequestBody WorksRequest request ,HttpServletResponse response){
        byte[] bytes = wxMiniProgramUtil.getUnlimited("targetId=&worksId="+request.getId()+"&from=share", "/pages/mine/paintList");
        responseImg(bytes, response,"image/jpeg");
    }


    @PostMapping("/complaintList")
    @ApiOperation(value = "投诉列表" ,notes = "")
    public SuperResponse complaintList(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.complaintList(request);
    }


    @PostMapping("/trialList")
    @ApiOperation(value = "待审作品" ,notes = "")
    public SuperResponse  trialList(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.trialList(request);
    }


    @PostMapping("/recoveryList")
    @ApiOperation(value = "回收站" ,notes = "")
    public SuperResponse recoveryList(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.recoveryList(request);
    }


    @PostMapping("/recovery")
    @ApiOperation(value = "回收站恢复" ,notes = "")
    public SuperResponse recovery(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.recovery(request);
    }

    @PostMapping("/recoveryDel")
    @ApiOperation(value = "回收站删除" ,notes = "")
    public SuperResponse recoveryDel(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.recoveryDel(request);
    }


    @PostMapping("/uploadAudio")
    @ApiOperation(value = "上传音频" ,notes = "")
    public SuperResponse uploadAudio(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.uploadAudio(request);
    }

    private void responseImg(byte[] bytes, HttpServletResponse response, String contentType) {
        InputStream in = new ByteArrayInputStream(bytes);
        try {
            OutputStream out = response.getOutputStream();
            response.setContentType(contentType);
            IOUtils.copy(in, out);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            IOUtils.closeQuietly(in);
        }
    }



    //======================== 移动端审核
    @PostMapping("/setAuthor")
    @ApiOperation(value = "设置作者" ,notes = "")
    public SuperResponse setAuthor(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.setAuthor(request);
    }



    //======================== 移动端审核
    @PostMapping("/reject")
    @ApiOperation(value = "作品审核拒绝" ,notes = "")
    public SuperResponse reject(@Valid @RequestBody WorksRequest request ){
        return paintingWorksService.reject(request);
    }


    @PostMapping("/rejectList")
    @ApiOperation(value = "被拒列表" ,notes = "")
    public SuperResponse rejectList(@Valid @RequestBody WorksListRequest request ){
        return paintingWorksService.rejectList(request);
    }

}


